#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    server = new QTcpServer;

    server->listen(QHostAddress::AnyIPv4,PORT);

    //客户端发起连接，server发出信号
    connect(server,&QTcpServer::newConnection,this,&Widget::newClientHandler);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::newClientHandler()
{
    //建立TCP连接
    QTcpSocket *socket = server->nextPendingConnection();
    socket->peerAddress();  //获取客户端ip地址
    socket->peerPort(); //获取客户端自己的端口

    ui->ipLineEdit->setText(socket->peerAddress().toString());
    ui->portLineEdit->setText(QString::number(socket->peerPort()));

    //服务器收到客户端发送的信息，socket发出readyread信号
    //connect(socket,&QTcpSocket::readyRead,this,&Widget::clientInfoSlot);

    //启动线程
    myThread *t = new myThread(socket);
    t->start();     //开启线程

    connect(t,&myThread::sendToWidget,this,&Widget::threadSlot);
}


// void Widget::clientInfoSlot()
// {
//     //获取信号的发送者
//     QTcpSocket *s = (QTcpSocket *)sender();
//     ui->mainLineEdit->setText(QString(s->readAll()));
// }

void Widget::threadSlot(QByteArray b)
{
    ui->mainLineEdit->setText(QString(b));
}
